import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

/// 提示框
class Loading {
  static const int _milliseconds = 500; // 提示 延迟毫秒, 提示体验 秒关太快
  static const int _dismissMilliseconds = 1000; // dismiss 延迟毫秒
  static final Color _defaultBgColor = Colors.black.withOpacity(0.7); // 背景颜色
  static List<BoxShadow>? _boxShadow;
  // 记录上一次toast弹出时间（默认时间是前一天）
  static DateTime _toastTime = DateTime.now().add(const Duration(days: -1));

  /// 初始化的
  Loading() {
    EasyLoading.instance
      ..displayDuration =
          const Duration(milliseconds: _dismissMilliseconds) // 关闭延迟
      ..indicatorType = EasyLoadingIndicatorType.ring // 指示器类型
      ..loadingStyle = EasyLoadingStyle.custom // loading样式 自定义
      ..indicatorSize = 35.0 // 指示器大小
      ..lineWidth = 2 // 进度条宽度
      ..radius = 10.0 // 圆角
      ..progressColor = Colors.white // 进度条颜色
      ..backgroundColor = _defaultBgColor // 背景颜色
      ..indicatorColor = Colors.white // 指示器颜色
      ..textColor = Colors.white // 文字颜色
      ..maskColor = Colors.black.withOpacity(0.6) // 遮罩颜色
      ..userInteractions = true // 用户交互
      ..dismissOnTap = false; // 点击关闭
    /// 记录初始化的boxShadow
    _boxShadow = EasyLoading.instance.boxShadow;
  }

  // 显示 (废弃使用，由Loading.request()替代，日后删除屏蔽代码)
  // static void show([String? text, Widget? widget]) {
  //   restoreDefault();
  //   EasyLoading.instance.userInteractions = false; // 屏蔽交互操作
  //   EasyLoading.show(status: text ?? '加载中...', indicator: widget);
  // }

  // 错误
  static void error([String? text]) {
    restoreDefault();
    Future.delayed(
      const Duration(milliseconds: _milliseconds),
      () => EasyLoading.showError(text ?? 'Error'),
    );
  }

  // 成功
  static void success([String? text]) {
    restoreDefault();
    Future.delayed(
      const Duration(milliseconds: _milliseconds),
      () => EasyLoading.showSuccess(text ?? 'Success'),
    );
  }

  /// 提示窗
  /// [text]提示文本;
  /// [type]弹出样式，0默认居中, 1居底, 2居顶;
  /// [shake]是否忽略抖动, 默认false, 设置为true可连续弹出
  /// [inSeconds] 防抖时长, 默认1s
  static void toast(String text, {bool? shake = false, int? inSeconds = 1}) {
    if (shake == true) {
      EasyLoading.showToast(text);
    } else {
      // 计算时间差
      Duration diff = DateTime.now().difference(_toastTime);
      // 防抖
      if (diff.inSeconds > inSeconds!) {
        EasyLoading.showToast(text);
        // 更新显示toast时间
        _toastTime = DateTime.now();
      }
    }
  }

  /// 关闭请求loading(设置关闭动画时长milliseconds，默认2000毫秒)
  static Future<void> dismiss({int? milliseconds}) async {
    await Future.delayed(
      Duration(milliseconds: milliseconds ?? 0),
      () {
        EasyLoading.instance.userInteractions = true; // 恢复交互操作
        /// 还原设置
        EasyLoading.dismiss().whenComplete(() => restoreDefault());
      },
    );
  }

  /// 恢复初始化设置
  static void restoreDefault() {
    // EasyLoading.instance
    //   ..backgroundColor = _defaultBgColor// 恢复默认背景颜色(请求loading存在无背景修改)
    //   ..maskType = EasyLoadingMaskType.none
    //   ..boxShadow = _boxShadow;

    EasyLoading.instance
      ..displayDuration =
          const Duration(milliseconds: _dismissMilliseconds) // 关闭延迟
      ..maskType = EasyLoadingMaskType.none
      ..indicatorType = EasyLoadingIndicatorType.ring // 指示器类型
      ..loadingStyle = EasyLoadingStyle.custom // loading样式 自定义
      ..indicatorSize = 35.0 // 指示器大小
      ..lineWidth = 2 // 进度条宽度
      ..radius = 10.0 // 圆角
      ..progressColor = Colors.white // 进度条颜色
      ..backgroundColor = _defaultBgColor // 背景颜色
      ..indicatorColor = Colors.white // 指示器颜色
      ..textColor = Colors.white // 文字颜色
      ..maskColor = Colors.black.withOpacity(0.6) // 遮罩颜色
      ..userInteractions = true // 用户交互
      ..dismissOnTap = false; // 点击关闭
    /// 记录初始化的boxShadow
    _boxShadow = EasyLoading.instance.boxShadow;
  }

  /// 显示请求加载Loading
  /// [text]加载泡展示文本
  /// [no]不要遮罩效果，默认不要遮罩
  static void request({String? text, bool? no = true}) {
    EasyLoading.instance
      ..loadingStyle = EasyLoadingStyle.custom // 加载样式
      ..maskType = no == true
          ? EasyLoadingMaskType.clear
          : EasyLoadingMaskType.black // 遮罩样式
      ..backgroundColor = Colors.transparent
      ..textColor = (no == true? Colors.black : Colors.white)// 文字颜色
      ..boxShadow = <BoxShadow>[]
      /// 解决背景色设置透明不生效
      ..userInteractions = false // 禁止交互
      ..dismissOnTap = false; // 禁止点击隐藏

    EasyLoading.show(
      status: text ?? '',
      // indicator: ColorLoader3(),
    );
  }
}
